package com.cmccsi.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.cmccsi.entity.qxdomainDto;
import com.cmccsi.mapper.qxdomainDtoMapper;
import com.cmccsi.mapper.qxuserDtoMapper;
import com.cmccsi.service.DomainService;
import com.cmccsi.utils.MybatisUtil;

public class DomainServiceImpl implements DomainService {

	private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();

	@Override
	public Boolean delete(String domainId) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxdomainDtoMapper domainMapper = session
					.getMapper(qxdomainDtoMapper.class);
			qxuserDtoMapper qxUserMapper = session
					.getMapper(qxuserDtoMapper.class);
			// 需要实现删除部门 人员信息
			Boolean r = qxUserMapper.deleteByDomainId(domainId);
			if (r) {
				domainMapper.deleteByPrimaryKey(domainId);
				return true;
			}
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean save(qxdomainDto domain) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxdomainDtoMapper domainMapper = session
					.getMapper(qxdomainDtoMapper.class);

			List<qxdomainDto> lstDm = domainMapper.selectByEcodeOrdomainName(
					domain.getEccode(), domain.getDomainName());
			if (lstDm == null || lstDm.size() == 0) {
				if (domain.getDomain().isEmpty()) {
					domain.setDomain(Integer.toString(Integer
							.parseInt(domainMapper.selectMaxPrimaryKey()) + 1));
				}
				domainMapper.insert(domain);
				session.commit();
				return true;
			} else {

				return update(domain);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean update(qxdomainDto domain) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxdomainDtoMapper domainMapper = session
					.getMapper(qxdomainDtoMapper.class);
			List<qxdomainDto> lstDm = domainMapper.selectByEcodeOrdomainName(
					domain.getEccode(), domain.getDomainName());
			if (lstDm != null && lstDm.size() > 0) {
				domain.setDomain(lstDm.get(0).getDomain());
				domainMapper.updateByPrimaryKey(domain);
				session.commit();
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public qxdomainDto find(String domainId) {
		// TODO Auto-generated method stub
		qxdomainDto domain = null;
		SqlSession session = sessionFactory.openSession();
		try {
			qxdomainDtoMapper domainMapper = session
					.getMapper(qxdomainDtoMapper.class);
			domain = domainMapper.selectByPrimaryKey(domainId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return domain;
	}

	@Override
	public String findMaxDomainId() {
		// TODO Auto-generated method stub
		String MaxdomainId = null;
		SqlSession session = sessionFactory.openSession();
		try {
			qxdomainDtoMapper domainMapper = session
					.getMapper(qxdomainDtoMapper.class);
			MaxdomainId = domainMapper.selectMaxPrimaryKey();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return MaxdomainId;
	}

	@Override
	public List<qxdomainDto> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

}
